package com.loongtech.bi.action.system;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.loongtech.bi.entity.system.EntitySysFunction;
import com.loongtech.bi.entity.system.EntitySysRole;
import com.loongtech.bi.entity.system.EntitySysRoleFunction;
import com.loongtech.bi.manager.system.SysFunctionManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.manager.system.SysRoleFunctionManager;
import com.loongtech.bi.manager.system.SysRoleManager;
import com.loongtech.bi.support.Session;

/**
 * 单个角色功能设置
 * 
 * @author xckuang
 * @date 2014-10-08
 */
@Controller("roleFuncManager")
@Scope("view")
public class RoleFuncManager implements Serializable {
	private static final long serialVersionUID = 1L;
	@Autowired
	private Session session;
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private SysRoleManager sysRoleManager;
	@Autowired
	private SysFunctionManager sysFunctionManager;
	@Autowired
	private SysRoleFunctionManager sysRoleFunctionManager;

	private Map<Integer, Boolean> selected = new HashMap<Integer, Boolean>();
	private Map<Integer, Boolean> unselected = new HashMap<Integer, Boolean>();
	private int roleId;
	private Map<Integer, EntitySysRole> roleMap = new HashMap<Integer, EntitySysRole>();
	private List<EntitySysFunction> leftList = new ArrayList<EntitySysFunction>(); // 可选择列表
	private List<EntitySysFunction> rightList = new ArrayList<EntitySysFunction>();// 已经选择列表

	@Autowired
	public void init() {
		roleMap.clear();
		roleId = 0;
		for (EntitySysRole role : sysRoleManager.findAll()) {
			roleMap.put(role.getId(), role);
		}
		change( );
	}

	public void select() {
		for (EntitySysFunction user : leftList) {
			Boolean val = selected.get(user.getId());
			if (null != val && val.booleanValue()) {
				rightList.add(user);
			}
		}
		selected.clear();
		leftList.removeAll(rightList);
	}

	public void unSelect() {
		for (EntitySysFunction user : rightList) {
			Boolean val = unselected.get(user.getId());
			if (null != val && val.booleanValue()) {
				leftList.add(user);
			}
		}
		unselected.clear();
		rightList.removeAll(leftList);
	}

	/**
	 * 保存角色用户信息
	 */
	public void save() {
		FacesContext context = FacesContext.getCurrentInstance();
		EntitySysRole role = sysRoleManager.get(roleId);
		if(null == role){
			context.addMessage(null, new FacesMessage("修改角色功能失败", "修改角色功能失败，角色不存在 roleId="+roleId));
			return;
		}
		// 当前集合
		List<EntitySysRoleFunction> list = sysRoleFunctionManager.findBy(EntitySysRoleFunction.K.roleId, roleId);
		Set<Integer> oldSet = new HashSet<Integer>();
		if (null != list && !list.isEmpty()) {
			for (EntitySysRoleFunction info : list) {
				oldSet.add(info.getFunctionId());
			}
		}
		// newSet of functionId
		Set<Integer> newSet = getSet(rightList);
		Set<Integer> submit = new HashSet<Integer>(oldSet);
		submit.retainAll(newSet);
		oldSet.removeAll(submit); // to remove
		newSet.removeAll(submit); // to insert

		// 删除oldSet
		for (int funcId : oldSet) {
			EntitySysRoleFunction entity = sysRoleFunctionManager.getBy(EntitySysRoleFunction.K.roleId, roleId,	EntitySysRoleFunction.K.functionId, funcId);
			if (null != entity) {
				sysRoleFunctionManager.removeById(entity.getId());
			}
		}

		// 插入newSet
		for (int funcId : newSet) {
			if (null == sysRoleFunctionManager.getBy(EntitySysRoleFunction.K.roleId, roleId, EntitySysRoleFunction.K.functionId, funcId)) {
				EntitySysRoleFunction entity = new EntitySysRoleFunction();
				entity.setRoleId(roleId);
				entity.setFunctionId(funcId);
				sysRoleFunctionManager.persist(entity);
			}
		}
		sysLogManager.addLog(session.getUserName(), "修改角色功能信息列表成功"+role.getName());
		context.addMessage(null, new FacesMessage("修改角色功能成功", "修改角色功能成功，角色名称为="+role.getName()));
		change();
	}

	private Set<Integer> getSet(List<EntitySysFunction> list) {
		Set<Integer> curSet = new HashSet<Integer>();
		if (null != list && !list.isEmpty()) {
			for (EntitySysFunction info : list) {
				if (null != sysFunctionManager.get(info.getId())) {
					curSet.add(info.getId());
				}
			}
		}
		return curSet;
	}
	
	public void change() {
		selected.clear();
		unselected.clear();
		EntitySysRole role = sysRoleManager.get(roleId);
		if (null == role) {
			leftList.clear();
			rightList.clear();
			leftList.addAll(sysFunctionManager.findAll());
			return;
		}

		// 先初始化rightList
		rightList.clear();
		List<EntitySysRoleFunction> list = sysRoleFunctionManager.findBy(EntitySysRoleFunction.K.roleId, roleId);
		if (null != list && !list.isEmpty()) {
			for (EntitySysRoleFunction info : list) {
				EntitySysFunction func = sysFunctionManager.get(info.getFunctionId());
				if (null != func) {
					rightList.add(func);
				}
			}
		}

		leftList.clear();
		leftList.addAll(sysFunctionManager.findAll());
		leftList.removeAll(rightList);
	}

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	public Map<Integer, EntitySysRole> getRoleMap() {
		return roleMap;
	}

	public void setRoleMap(Map<Integer, EntitySysRole> roleMap) {
		this.roleMap = roleMap;
	}

	public Map<Integer, Boolean> getSelected() {
		return selected;
	}

	public void setSelected(Map<Integer, Boolean> selected) {
		this.selected = selected;
	}

	public Map<Integer, Boolean> getUnselected() {
		return unselected;
	}

	public void setUnselected(Map<Integer, Boolean> unselected) {
		this.unselected = unselected;
	}

	public List<EntitySysFunction> getLeftList() {
		return leftList;
	}

	public void setLeftList(List<EntitySysFunction> leftList) {
		this.leftList = leftList;
	}

	public List<EntitySysFunction> getRightList() {
		return rightList;
	}

	public void setRightList(List<EntitySysFunction> rightList) {
		this.rightList = rightList;
	}
}